﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation.DefaultProjectInternals
{
    using System;
    using System.Drawing;
    using System.IO;
    using ConnectBasic.Shell.Extensibility;
    using ConnectBasic.Shell.Hosting;

    internal class ItemTypeDef
    {
        private string mImageResourceName;
        private Image mImage;
        private Func<ItemTypeDef, DefaultProject, IProjectStoreItem, IProjectItem> mCreateMethod;
        private Func<ItemTypeDef, DefaultProject, IProjectStoreItem, IProjectItem> mLoadMethod;

        internal ItemTypeDef(string imageResourceName, Func<ItemTypeDef, DefaultProject, IProjectStoreItem, IProjectItem> createMethod, Func<ItemTypeDef, DefaultProject, IProjectStoreItem, IProjectItem> loadMethod)
        {
            mImageResourceName = imageResourceName;
            mCreateMethod = createMethod;
            mLoadMethod = loadMethod;
        }

        public Image GetSmallImage()
        {
            if (mImage == null && mImageResourceName != null)
            {
                using (Stream imageStream = typeof(ItemTypeDef).Assembly.GetManifestResourceStream(mImageResourceName))
                {
                    if (imageStream != null)
                    {
                        mImage = Image.FromStream(imageStream);
                    }
                }
            }

            return mImage;
        }

        public IProjectItem CreateItem(DefaultProject project, IProjectStoreItem itemStore)
        {
            return mCreateMethod(this, project, itemStore);
        }

        public IProjectItem LoadItem(DefaultProject project, IProjectStoreItem itemStore)
        {
            return mLoadMethod(this, project, itemStore);
        }
    }
}
